<?php


namespace app\admin\controller;


use app\common\model\AuthGroup;
use app\common\model\ConfigGroup as CG;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class ConfigGroup  extends Base
{
    public function index(){
        $pid = Request::param('pid'); //类别pid
        $where = Request::param('where', []);
        $where = array_filter($where);
        $keyword = Request::param('keyword');

        $list = CG::where($where)
            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->whereLike('group_name|group_title', "%$keyword%");
                }
            })
            ->order('group_sort asc')
            ->paginate(10, false, ['query' => request()->param()]);

        $page = $list->render();
        $view = [
            'keyword' => $keyword,
            'page' => $page,
            'list' => $list,
            'pid' => $pid,
        ];
        View::assign($view);
        return View::fetch();
    }


    public function add(){
        if (Request::isPost()){
            $data = Request::post();
            $data['add_time'] = time();
            $data['uid'] = Session::get('admin.admin_id');
            if (CG::create($data)){
                $this->success('添加配置组名称:' . $data['group_title'].'添加成功', url('index'));
            }else{
                $this->error('添加失败');
            }
        }
        return View::fetch();
    }

    public function edit(){
        $group_id = Request::param('group_id');
        if(Request::isPost()){
            $data = Request::post();
            $where['group_id'] = $group_id;
            CG::update($data,$where);
            $this->success('修改成功!', 'index');
        }else{
            $group_one = Db::name('config_group')->where(array('group_id'=>$group_id))->find();
            $view = [
                'group_one'=>$group_one
            ];
        }
        View::assign($view);
        return View::fetch();
    }

    public function del(){
        $group_id = Request::param('group_id');
        Db::name('config')->where('group_id',$group_id)->delete();
        if (Db::name('config_group')->delete($group_id)){
            return success('删除配置组成功！','index');
        }else{
            return error('删除配置组失败！');
        }
    }

    public function pildel(){
        $id = Request::param('id');
        $ids = explode(',',$id);
        foreach ($ids as $k =>$v){
            Db::name('config')->where('group_id',$v)->delete();
            $res = Db::name('config_group')->where('group_id',$v)->delete();
        }
        if($res){
            return success('删除配置组成功！','index');
        }else{
            return error('删除配置组失败！');
        }
    }

    public function sort()
    {
        if (Request::isPost()) {
            $group_id = Request::param('group_id');
            $group_sort = Request::param('group_sort');
            if(Db::name('config_group')->where(array('group_id'=>$group_id))->update(['group_sort' => $group_sort])){
                $this->success('排序成功', 'index');
            }
        }
    }





    public function ajaxGroupName ()
    {
        $key = Request::param('name');
        $value = Request::param('param');
        $group_id = input('group_id', 0, 'intval');
        $res = ConfigGroup::ajaxGroupNames($value, $group_id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '配置组英文名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }

    public function ajaxGroupNames($group_name,$group_id = 0){
        if(empty($group_name)){
            return 1;
        }
        //组装where条件
        $where = array();
        $where[] = array('group_name','=',$group_name);
        if($group_id > 0){
            $where[] =  array('group_id','<>',$group_id);
        }
        return Db::name('config_group')->where($where)->find();
    }
    public function ajaxGroupTitle ()
    {
        $key = Request::param('name');
        $value = Request::param('param');
        $group_id = Request::param('group_id', 0, 'intval');
        $res = ConfigGroup::ajaxGroupTitles($value, $group_id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '配置组中文名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }
    public function ajaxGroupTitles($group_title,$group_id = 0){
        if(empty($group_title)){
            return 1;
        }
        //组装where条件
        $where = array();
        $where[] = array('group_title','=',$group_title);
        if($group_id > 0){
            $where[] =  array('group_id','<>',$group_id);
        }
        return Db::name('config_group')->where($where)->find();
    }
}